package com.hikvision.open.app.ui.playback

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.hikvision.open.app.model.PlayStatus
import com.hikvision.open.app.model.RecordSegment
import com.hikvision.open.app.ui.preview.PlayWindowView
import com.hikvision.open.app.utils.CalendarUtil
import com.hikvision.open.app.utils.inflate
import com.hikvision.open.app.widget.TimeBarView
import hik.sdk.demo.databinding.ActivityPlaybackBinding

/**
 * <p> 录像回放界面 </p>
 * @author 段自航 2021/7/17 13:41
 * @version V1.0
 */
class PlaybackActivity : AppCompatActivity() {

    private val viewBinding: ActivityPlaybackBinding by inflate()

    private val groupHelper by lazy {
        viewBinding.windowGroup.windowGroupAdapter
    }

    private var hasStartUpdate = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        initView()
        initTimeBarView()
    }


    private fun initView() {
        viewBinding.backImage.setOnClickListener {
            finish()
        }
        //默认选中第一个窗口
        viewBinding.windowGroup.selectedCurrFirstWindow()
        groupHelper.allDoubleClickEnable = false
        viewBinding.playbackControlView.setWindowGroupHelper(groupHelper)
    }

    private fun initTimeBarView() {
        //todo 注意，这里是模拟的假数据，表示当天的时间
        val recordSegment = RecordSegment()
        val defaultStartCalendar = CalendarUtil.getDefaultStartCalendar()
        recordSegment.beginTime =
            CalendarUtil.calendarToyyyy_MM_dd_T_HH_mm_SSSZ(defaultStartCalendar)
        val curDayEndTime = CalendarUtil.getCurDayEndTime(defaultStartCalendar.timeInMillis)
        defaultStartCalendar.timeInMillis = curDayEndTime
        recordSegment.endTime = CalendarUtil.calendarToyyyy_MM_dd_T_HH_mm_SSSZ(defaultStartCalendar)
        viewBinding.timeBarView.addFileInfoList(mutableListOf(recordSegment))
        viewBinding.timeBarView.setTimeBarCallback(object : TimeBarView.TimePickedCallBack {
            override fun onMoveTimeCallback(currentTime: Long) {

            }

            override fun onBarMoving(currentTime: Long) {

            }

            override fun onTimePickedCallback(currentTime: Long) {
                groupHelper.currentWindowItem?.let {
                    val playWindowView = it as PlayWindowView
                    if (playWindowView.getPlayStatus() == PlayStatus.IDLE) {
                        return@let
                    }
                    playWindowView.seekPlayback(currentTime)
                }
            }

            override fun onMaxScale() {

            }

            override fun onMinScale() {

            }
        })
    }

    override fun onResume() {
        super.onResume()
        if (!hasStartUpdate) {
            hasStartUpdate = true
            startUpdate()
        }
    }

    /**
     * 开始刷新流量
     */
    private fun startUpdate() {
        viewBinding.root.postDelayed(getOSDTimeTask, 400L)
    }

    /**
     * 停止刷新流量
     */
    private fun cancelUpdate() {
        viewBinding.root.removeCallbacks(getOSDTimeTask)
    }

    private val getOSDTimeTask = Runnable {
        groupHelper.currentWindowItem?.let {
            val playWindowView = it as PlayWindowView
            val osdTime: Long = playWindowView.getOSDTime()
            Log.e("PlaybackActivity", "osdTime is $osdTime")
            if (osdTime > -1) {
                viewBinding.timeBarView.setCurrentTime(osdTime)
            }

            startUpdate()
        }

    }

    override fun onRestart() {
        super.onRestart()
        //界面重新展示时，通知窗口可见了
        groupHelper.setPageChangeEvent(groupHelper.currentPage, groupHelper.windowMode)
    }


    override fun onStop() {
        super.onStop()
        cancelUpdate()
        hasStartUpdate = false
        //界面到后台时，通知窗口不可见了
        groupHelper.windowItemStructAllList.forEach {
            if (it.userVisibleHint) {
                it.userVisibleHint = false
            }
        }
    }
}